package Game;

import Helper.ArrayHelper;

public abstract class AbstractEffect {
	// CONSTANTS
	
	// Type constants, i.e. object which the Effect modifies.
	public static final int TYPE_UNIT = 0;
	public static final int TYPE_DAMAGE = 1;
	
	// ATTRIBUTES
	String name;
	String desc;
	
	Event[] triggers;
	boolean autoActive = true;
	int duration;
	
	// METHODS
	public AbstractEffect(String _name, String _desc, int _duration) {
		initEffect(_name, _desc, _duration);
		triggers = new Event[1];
		triggers[0] = new Event(Event.EVENT_OTHER);
	}
	
	public AbstractEffect(String _name, String _desc, int _duration, Event[] _triggers) {
		initEffect(_name, _desc, _duration);
		triggers = ArrayHelper.cloneArray(_triggers);
	}
	
	private void initEffect(String _name, String _desc, int _duration) {
		name = _name;
		desc = _desc;
		duration = _duration;
	}
	
	public void onObtain(IAffectable affectable) {
		affectable.effects.add(this);
		
		if ((type == TYPE_UNIT) && (autoActive || isUnitMetCondition(unit))) {
			onActivate(affectable);
		}
	}
	
	public void onDismiss(IAffectable affectable) {
		if ((type == TYPE_UNIT) && (autoActive || isUnitMetCondition(unit))) {
			onDeactivate(unit);
		}
		unit.effects.remove(this);
	}
	
	public abstract void onActivate(IEffectTarget affectable);
	
	public void onDeactivate(Unit unit) { }
	
	public boolean isConditionMet(IEffectTarget effectTarget) {
		for (int event: )
		
		switch (type) {
		case TYPE_UNIT:
			return isUnitMetCondition((Unit)effectTarget);
		case TYPE_DAMAGE:
			return isDamageMetCondition((Damage)effectTarget);
		default:
			return false;
		}
	}
	
	public boolean isUnitMetCondition(Unit unit) {
		return true;
	}
	
	public boolean isDamageMetCondition(Damage damage) {
		for (int i = 0; i < triggers.length; i++) {
			if (triggers[i]) {
				switch (i) {
				case TRIGGER_DAMAGED:
					if (!damage.healing)
						return true;
				case TRIGGER_HEALED:
					if (damage.healing)
						return true;
					
				case TRIGGER_PHYSICAL_SKILL:
					if (damage.fromSkill && !damage.magical)
						return true;
				case TRIGGER_MAGICAL_SKILL:
					if (damage.fromSkill && damage.magical)
						return true;
				case TRIGGER_HEALING:
					if (damage.healing)
						return true;
					
				case TRIGGER_NORMAL_ATTACK:
					if (!damage.fromSkill && (damage.element == Element.ELEMENT_NORMAL))
						return true;
				case TRIGGER_FIRE_ATTACK:
					if (!damage.fromSkill && (damage.element == Element.ELEMENT_FIRE))
						return true;
				case TRIGGER_WIND_ATTACK:
					if (!damage.fromSkill && (damage.element == Element.ELEMENT_WIND))
						return true;
				case TRIGGER_EARTH_ATTACK:
					if (!damage.fromSkill && (damage.element == Element.ELEMENT_EARTH))
						return true;
				case TRIGGER_THUNDER_ATTACK:
					if (!damage.fromSkill && (damage.element == Element.ELEMENT_THUNDER))
						return true;
				case TRIGGER_WATER_ATTACK:
					if (!damage.fromSkill && (damage.element == Element.ELEMENT_WATER))
						return true;
				case TRIGGER_HOLY_ATTACK:
					if (!damage.fromSkill && (damage.element == Element.ELEMENT_HOLY))
						return true;
				case TRIGGER_DARKNESS_ATTACK:
					if (!damage.fromSkill && (damage.element == Element.ELEMENT_DARKNESS))
						return true;
					
				case TRIGGER_NORMAL_MAGIC:
					if (damage.magical && (damage.element == Element.ELEMENT_NORMAL))
						return true;
				case TRIGGER_FIRE_MAGIC:
					if (damage.magical && (damage.element == Element.ELEMENT_FIRE))
						return true;
				case TRIGGER_WIND_MAGIC:
					if (damage.magical && (damage.element == Element.ELEMENT_WIND))
						return true;
				case TRIGGER_EARTH_MAGIC:
					if (damage.magical && (damage.element == Element.ELEMENT_EARTH))
						return true;
				case TRIGGER_THUNDER_MAGIC:
					if (damage.magical && (damage.element == Element.ELEMENT_THUNDER))
						return true;
				case TRIGGER_WATER_MAGIC:
					if (damage.magical && (damage.element == Element.ELEMENT_WATER))
						return true;
				case TRIGGER_HOLY_MAGIC:
					if (damage.magical && (damage.element == Element.ELEMENT_HOLY))
						return true;
				case TRIGGER_DARKNESS_MAGIC:
					if (damage.magical && (damage.element == Element.ELEMENT_DARKNESS))
						return true;
					
				case TRIGGER_NORMAL_DAMAGED:
					if (!damage.healing && (damage.element == Element.ELEMENT_NORMAL))
						return true;
				case TRIGGER_FIRE_DAMAGED:
					if (!damage.healing && (damage.element == Element.ELEMENT_FIRE))
						return true;
				case TRIGGER_WIND_DAMAGED:
					if (!damage.healing && (damage.element == Element.ELEMENT_WIND))
						return true;
				case TRIGGER_EARTH_DAMAGED:
					if (!damage.healing && (damage.element == Element.ELEMENT_EARTH))
						return true;
				case TRIGGER_THUNDER_DAMAGED:
					if (!damage.healing && (damage.element == Element.ELEMENT_THUNDER))
						return true;
				case TRIGGER_WATER_DAMAGED:
					if (!damage.healing && (damage.element == Element.ELEMENT_WATER))
						return true;
				case TRIGGER_HOLY_DAMAGED:
					if (!damage.healing && (damage.element == Element.ELEMENT_HOLY))
						return true;
				case TRIGGER_DARKNESS_DAMAGED:
					if (!damage.healing && (damage.element == Element.ELEMENT_DARKNESS))
						return true;
				}
			}
		}
		
		return false;
	}
	
	public boolean isUnitEffect() {
		return (type == TYPE_UNIT);
	}
	
	public boolean isDamageEffect() {
		return (type == TYPE_DAMAGE);
	}
	
	@Override
	public String toString() {
		String s = "";
		
		s += name + "\n" + desc;
		
		return s;
	}
}
